/**
 * Created by sun on 2017/3/28.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    require('angular-ueditor');
    require('ZeroClipboard');

    var toastr =require('toastr');

    app.useModule("ui.table");
    app.useModule("ng.ueditor");

    var datepicker = require('datepicker');
    app.controller('packMan_firstCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.ctrlScope=$scope;
        datepicker($scope);
        $scope.orderParams2={};
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };

        //包列表
        $scope.orderParams = {
            url: "/admin/asset/getPackageList",
            ajax:{status:1}
        };

        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime= $scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime= $scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.ajaxParam={status:1};
            $scope.orderParams.reload();
        };

        //标列表
        $scope.orderParams2 = {
            url: "/admin/asset/getItemListByPackageId"
        };

        //包审核
        $scope.modifyModal=function(id){
            $scope.checkSubmitFlg=false;
            $scope.ctrlScope.first={};
            $scope.showPage = !$scope.showPage;
            $scope.ctrlScope.packageId=id;
            $scope.orderParams2.ajaxParam.packageId=id
            $scope.orderParams2.reload();
        }

        //审核 / 编辑 提交
        $scope.submitForm = function(isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
                return;

            }
            if($scope.checkSubmitFlg ==true){
                return false; //当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            }
            $scope.checkSubmitFlg =true;
            var datajson = $scope.ctrlScope.first;
            datajson.packageId = $scope.ctrlScope.packageId;
                console.log(datajson)
            $http({
                method: 'POST',
                url: "/admin/asset/packageFirstTrial",
                data: datajson
            }).success(function(data) {
                console.log(data)
                if(data.result=="success"){
                    toastr.success("提交成功");
                    $scope.showPage = !$scope.showPage;
                    $scope.reset();
                }else{
                    if(data.respMsg){
                        toastr.error(data.respMsg);
                    }else{
                        toastr.error(data.messageText);
                    }

                }
            }).error(function() {
            });
        };

    }]);


});